package cn.codesea.housework_user.ui.manager;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ManagerService;
import cn.codesea.housework_user.net.service.WorkerService;
import cn.codesea.housework_user.ui.BaseActivity;
import cn.codesea.housework_user.ui.worker.MainWorkerActivity;
import cn.codesea.housework_user.ui.worker.RegisterWorkerActivity;
import cn.codesea.housework_user.utils.SPUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ManagerLoginActivity extends BaseActivity{

    @BindView(R.id.username_et)
    EditText username_et;
    @BindView(R.id.pwd_et)
    EditText pwd_et;
    @BindView(R.id.login_btn)
    Button login_btn;

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_manager);
        ButterKnife.bind(this);
        init();
    }

    public void init(){
        initToolbar();
    }

    public void initToolbar(){
        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startActivity(new Intent(LoginWorkerActivity.this,MainWorkerActivity.class));
                finish();
            }
        });
    }

    @OnClick(R.id.login_btn)
    public void login_btn(){

        String username = username_et.getText().toString();
        String pwd = pwd_et.getText().toString();

        if(!"".equals(username)&&!"".equals(pwd)){
            ManagerService service = ServiceGenerator.createService(ManagerService.class);
            Call<Result> call = service.managerLogin(username,pwd);
            call.enqueue(new Callback<Result>(){
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    resultProcess(response.body());
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    Toast.makeText(ManagerLoginActivity.this,"网络错误!",Toast.LENGTH_SHORT).show();
                }
            });

        }else {
            Toast.makeText(ManagerLoginActivity.this,"账号或密码不能为空！",Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result){
        if (result.getState() == 200){
            SPUtil.loginSuccess(ManagerLoginActivity.this,String.valueOf(result.getRes()),SPUtil.MANAGER_LOGIN);
            startActivity(new Intent(ManagerLoginActivity.this,ManagerMainActivity.class));
            finish();
        }else {
            Toast.makeText(ManagerLoginActivity.this,"账号或密码错误！",Toast.LENGTH_SHORT).show();
        }
    }


}

